﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace DotNet.Common.Extensions
{
        public static partial class StringExtensions
        {
                /// <summary>
                /// 将对象转为包含自身的数组对象
                /// </summary>
                /// <typeparam name="T">对象类型</typeparam>
                /// <param name="obj"></param>
                /// <returns></returns>
                public static T[] AsArray<T>(this T obj)
                {
                        return new T[] { obj };
                }

                /// <summary>
                /// 将对象转为包含自身的List对象
                /// </summary>
                /// <typeparam name="T">对象类型</typeparam>
                /// <param name="obj"></param>
                /// <returns></returns>
                public static List<T> AsList<T>(this T obj)
                {
                        return new List<T> { obj };
                }

                #region System.Web.WebPages Copy过来的扩展方法

                public static int AsInt(this string value)
                {
                        return value.As<int>();
                }

                public static int AsInt(this string value, int defaultValue)
                {
                        return value.As(defaultValue);
                }

                public static decimal AsDecimal(this string value)
                {
                        return value.As<decimal>();
                }

                public static decimal AsDecimal(this string value, decimal defaultValue)
                {
                        return value.As(defaultValue);
                }

                public static float AsFloat(this string value)
                {
                        return value.As<float>();
                }

                public static float AsFloat(this string value, float defaultValue)
                {
                        return value.As(defaultValue);
                }

                public static DateTime AsDateTime(this string value)
                {
                        return value.As<DateTime>();
                }

                public static DateTime AsDateTime(this string value, DateTime defaultValue)
                {
                        return value.As(defaultValue);
                }

                public static TValue As<TValue>(this string value)
                {
                        return value.As(default(TValue));
                }

                public static bool AsBool(this string value)
                {
                        return value.As(false);
                }

                public static bool AsBool(this string value, bool defaultValue)
                {
                        return value.As(defaultValue);
                }

                public static TValue As<TValue>(this string value, TValue defaultValue)
                {
                        try
                        {
                                TypeConverter converter = TypeDescriptor.GetConverter(typeof(TValue));
                                if (converter.CanConvertFrom(typeof(string)))
                                {
                                        TValue result = (TValue)converter.ConvertFrom(value);
                                        return result;
                                }
                                converter = TypeDescriptor.GetConverter(typeof(string));
                                if (converter.CanConvertTo(typeof(TValue)))
                                {
                                        TValue result = (TValue)converter.ConvertTo(value, typeof(TValue));
                                        return result;
                                }
                        }
                        catch (System.Exception)
                        {
                        }
                        return defaultValue;
                }

                public static bool IsBool(this string value)
                {
                        return value.Is<bool>();
                }

                public static bool IsInt(this string value)
                {
                        return value.Is<int>();
                }

                public static bool IsDecimal(this string value)
                {
                        return value.Is<decimal>();
                }

                public static bool IsFloat(this string value)
                {
                        return value.Is<float>();
                }

                public static bool IsDateTime(this string value)
                {
                        return value.Is<DateTime>();
                }

                public static bool Is<TValue>(this string value)
                {
                        TypeConverter converter = TypeDescriptor.GetConverter(typeof(TValue));
                        return converter != null && converter.CanConvertFrom(typeof(string)) && converter.IsValid(value);
                }
                #endregion
        }
}
